<?php

define('DEFAULT_PHP_EXTENSION', '.php');

class Base_Cube
{
    static private $er;
    public function __construct()
    {
        //Stores the old error reporting configuration and sets a new one
        if (self::$er = ini_get('error_reporting')) error_reporting(E_ALL|E_STRICT);
        //Register a new autoload method
        spl_autoload_register(array('Base_Cube','_autoload'));
    }
    public static function _autoload($r)
    {
        //If a file matching the Base custom class name exists, require it and return true
        if (substr($r,0,5) == 'Base_' && file_exists($fileName = 'Base/'.substr($r,5).DEFAULT_PHP_EXTENSION)) {
            require_once $fileName;
            return TRUE;
        }
        
        //If a file stored matches the class call, require it
        if (file_exists($fileName = str_replace('_','/',$r).'.php')) {
            require_once $fileName;
            return TRUE;
        }
        
        //If the class matches a baseclass previously declared, extend it
        if (preg_match('#([[:alpha:]]*)_([[:alpha:]]*){1}$#', $r , $f) && class_exists('Base_'.$f[2])) {
            eval('class '.$r.' extends Base_'.$f[2].' {} ');
        }
        //If the loaded class has an autoload method, register it
        if (method_exists($r, '_autoload')) spl_autoload_register(array($r, '_autoload'));
    }
    public static function clean()
    {
        //Restores the old error_reporting setting
        if (!empty(self::$er)) error_reporting(self::$er);    
    }
}

//Returns the object on file includes
return new Base_Cube;
?>
